import { api } from './request';

// 试卷查询请求参数
export interface PaperQueryParams {
  page?: number;
  size?: number;
  title?: string;
  college?: string;
  subject?: string;
  year?: string;
  status?: 'pending' | 'approved' | 'rejected';
  uploadUserId?: number;
  startDate?: string;
  endDate?: string;
}

// 试卷信息
export interface PaperInfo {
  id: number;
  title: string;
  college: string;
  subject: string;
  year: string;
  description?: string;
  fileName: string;
  filePath: string;
  fileSize: number;
  status: 'pending' | 'approved' | 'rejected';
  uploadUserId: number;
  uploadUserNickname?: string;
  reviewerId?: number;
  reviewerNickname?: string;
  reviewComment?: string;
  uploadTime: string;
  reviewTime?: string;
  downloadCount: number;
  watermarkGenerated: boolean;
  watermarkType?: 'standard' | 'strict';
  watermarkPath?: string;
}

// 分页响应
export interface PageResponse<T> {
  content: T[];
  totalElements: number;
  totalPages: number;
  size: number;
  number: number;
  first: boolean;
  last: boolean;
  empty: boolean;
}

// 试卷审核请求
export interface PaperReviewRequest {
  status: 'approved' | 'rejected';
  comment?: string;
}

// 试卷批量操作请求
export interface PaperBatchRequest {
  paperIds: number[];
  action: 'approve' | 'reject' | 'delete';
  comment?: string;
}

// 水印配置请求
export interface WatermarkRequest {
  watermarkType: 'standard' | 'strict';
}

// 试卷管理API
export const paperApi = {
  // 获取试卷列表（分页查询）
  getPapers(params: PaperQueryParams): Promise<PageResponse<PaperInfo>> {
    return api.get('/api/admin/papers', params);
  },

  // 获取试卷详情
  getPaperDetail(paperId: number): Promise<PaperInfo> {
    return api.get(`/api/admin/papers/${paperId}`);
  },

  // 审核试卷
  reviewPaper(paperId: number, data: PaperReviewRequest): Promise<PaperInfo> {
    return api.post(`/api/admin/papers/${paperId}/review`, data);
  },

  // 删除试卷
  deletePaper(paperId: number): Promise<void> {
    return api.delete(`/api/admin/papers/${paperId}`);
  },

  // 批量操作试卷
  batchOperatePapers(data: PaperBatchRequest): Promise<void> {
    return api.post('/api/admin/papers/batch', data);
  },

  // 下载试卷文件
  downloadPaper(paperId: number, filename?: string): Promise<void> {
    return api.download(`/api/admin/papers/${paperId}/download`, filename);
  },

  // 生成/重新生成水印文件（URL参数方式）
  regenerateWatermark(paperId: number, watermarkType: 'standard' | 'strict'): Promise<PaperInfo> {
    return api.put(`/api/admin/papers/${paperId}/watermark?watermarkType=${watermarkType}`);
  },

  // 生成/重新生成水印文件（JSON请求体方式）
  regenerateWatermarkWithBody(paperId: number, data: WatermarkRequest): Promise<PaperInfo> {
    return api.post(`/api/admin/papers/${paperId}/watermark`, data);
  },

  // 下载水印文件
  downloadWatermark(paperId: number, filename?: string): Promise<void> {
    return api.download(`/api/admin/papers/${paperId}/watermark/download`, filename);
  },

  // 获取试卷统计信息
  getPaperStats(): Promise<{
    totalPapers: number;
    pendingPapers: number;
    approvedPapers: number;
    rejectedPapers: number;
    todayUploads: number;
    totalDownloads: number;
  }> {
    return api.get('/api/admin/papers/stats');
  },

  // 获取学院列表
  getColleges(): Promise<string[]> {
    return api.get('/api/admin/papers/colleges');
  },

  // 获取科目列表
  getSubjects(college?: string): Promise<string[]> {
    return api.get('/api/admin/papers/subjects', { college });
  },

  // 获取年份列表
  getYears(): Promise<string[]> {
    return api.get('/api/admin/papers/years');
  },
};

export default paperApi;